import datetime
import time
from Case.python_ui.Test_data.login_data import Logindata
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.product_management_locator import ProductManagementLocator
from Case.python_ui.Pageobject.admin_product_managent_pages import ProductManagementPages
from Case.python_ui.Common.commontool import CommonTool
from Case.python_ui.Test_data.common_data import Commondata
from Case.python_ui.Test_data.prod_data import Product
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import StaleElementReferenceException
from Case.python_ui.mylogging.mylogging import MyLogging
from Case.python_ui.Common.business_common_tool import BusinessCommonTools
from Case.python_ui.PageLocate.user_eip_locator import UserEIPLocator
from Case.python_ui.PageLocate.user_add_expired_date_locator import UserAddExpiredDate
from Case.python_ui.PageLocate.user_my_resource_locator import UserMyResourceLocator
from Case.python_ui.Pageobject.user_slb_pages import UserSLBPages
from Case.python_ui.PageLocate.user_slb_locator import UserSLBLocator
from selenium.common.exceptions import NoSuchElementException, ElementClickInterceptedException, \
    ElementNotInteractableException


class UserAddExpiredTime(Basepage):

    # 进入my resource界面
    def enter_my_resource_page(self):
        doc = '进入user端的myresource页面'
        for i in range(3):
            try:
                self.wait_elevisble(ProductManagementLocator.my_resource_tab, doc)
                self.click_element(ProductManagementLocator.my_resource_tab, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(ProductManagementLocator.page_total, doc)

    def get_prod_name_to_click(self, prod_name):
        doc = '获取产品名称然后点击'
        prod_list = self.get_elements(ProductManagementLocator.prod_list, doc)
        time.sleep(2)
        for prod in prod_list:
            if prod.text == prod_name:
                prod.click()
        time.sleep(2)

    # 校验某个属性值是否符合预期
    def check_attribute(self, real_result, except_result):
        if real_result == except_result:
            assert True
        else:
            assert False, '实际值:' + real_result + '期望值:' + except_result

    # 判断是否包含元素
    def check_contain_str(self, string, except_string):
        if except_string in string:
            assert True
        else:
            assert False, '实际值:' + string + '期望值:' + except_string


    def open_pay_pages(self, resource_name):
        doc = '打开资源购买页面'
        time.sleep(2)
        self.wait_elevisble(ProductManagementLocator.cerate_new_tab, doc)
        self.move_mouse_to_element(ProductManagementLocator.cerate_new_tab)
        time.sleep(2)
        self.get_prod_name_to_click(resource_name)
        time.sleep(2)
        self.switch_to_new_window()
        time.sleep(2)
        self.wait_elevisble(UserEIPLocator.pag_button, doc)
        self.click_element(UserEIPLocator.pag_button, doc)
        time.sleep(2)
        self.wait_elevisble(UserEIPLocator.expired_time, doc)
        result = self.get_element(UserEIPLocator.expired_time, doc).text
        self.check_attribute(result, 'Expired Time')
        time.sleep(2)


    def get_category_to_click(self,category):
        doc='获取分类配置然后点击'
        self.wait_elevisble(ProductManagementLocator.instance_category, doc)
        eles=self.get_elements(ProductManagementLocator.instance_category, doc)
        for ele in eles:
            if ele.text==category:
                ele.click()
        time.sleep(2)


    def button_is_enabled(self,locate):
        doc='判断按钮是否可以点击'
        button=self.get_element(locate,doc)
        for i in range(5):
            time.sleep(2)
            if  button.is_enabled():
                button.click()
                break



    # TL562024 | 购买时选择PAYG方式，查看页面显示
    def expired_time_check(self):
        self.enter_my_resource_page()
        time.sleep(2)
        doc = '购买时选择PAYG方式，查看页面显示'
        resource_list = ['Elastic IP Addresses', 'Server Load Balancer', 'Elastic Compute Service', 'NAT Gateways', 'Block Storage', 'VPN Gateways', 'RDS for MySQL', 'RDS for SQL Server']
        for resource in resource_list:
            self.open_pay_pages(resource)
            self.driver.close()
            all_handles = self.driver.window_handles
            self.driver.switch_to.window(all_handles[0])
            time.sleep(2)


    # TL562027 | 查看Expire time的注释和内容
    def expired_time_note(self):
        self.enter_my_resource_page()
        time.sleep(2)
        doc = '查看Expire time的注释和内容'
        text = 'You can preset the expired time for the Pay-As-You-Go instance. When the Pay-As-You-Go instance reaches the expired date, CRE system will automatically release the instance. And we will send a message to remind you before the expiration date, and you can change the expiration date in "My Resource" if necessary.'
        resource_list = ['Server Load Balancer', 'Elastic IP Addresses', 'NAT Gateways',
                         'Block Storage', 'VPN Gateways']
        for resource in resource_list:
            self.open_pay_pages(resource)
            self.move_mouse_to_element(UserAddExpiredDate.other_note)
            self.wait_elevisble(UserAddExpiredDate.note_text, doc)
            result = self.get_element(UserAddExpiredDate.note_text, doc).text
            self.check_attribute(result, text)
            self.driver.close()
            all_handles = self.driver.window_handles
            self.driver.switch_to.window(all_handles[0])
            time.sleep(2)


    def create_ecs_pay_as_you_go(self,cmdopt):
        doc='创建一个esc pay_as_you_go'
        self.driver.get(Commondata.home_page[cmdopt]['user'])
        self.enter_my_resource_page()
        self.wait_elevisble(ProductManagementLocator.cerate_new_tab, doc)
        self.move_mouse_to_element(ProductManagementLocator.cerate_new_tab)
        time.sleep(2)
        self.get_prod_name_to_click('Elastic Compute Service')
        time.sleep(2)
        self.switch_to_new_window()
        time.sleep(5)
        self.wait_elevisble(ProductManagementLocator.pay_as_you_go_button, doc)
        time.sleep(2)
        self.click_element(ProductManagementLocator.pay_as_you_go_button,doc)
        time.sleep(2)
        self.click_element(UserAddExpiredDate.instance_type, doc)
        self.wait_elevisble(ProductManagementLocator.instance_name_input, doc)
        self.send_keys(ProductManagementLocator.instance_name_input, 'test', doc)
        time.sleep(2)
        self.click_element(UserAddExpiredDate.image1, doc)
        time.sleep(2)
        self.click_element(UserAddExpiredDate.image1_input, doc)
        time.sleep(2)
        self.click_element(ProductManagementLocator.deployment_set_select, doc)
        self.wait_elevisble(ProductManagementLocator.deployment_set_option, doc)
        deployment_set = self.get_elements(ProductManagementLocator.deployment_set_option, doc)
        deployment_set[0].click()
        time.sleep(2)
        self.click_element(ProductManagementLocator.vpc_select, doc)
        self.wait_elevisble(ProductManagementLocator.vpc_option, doc)
        vpc_switch_eles = self.get_elements(ProductManagementLocator.vpc_option, doc)
        vpc_switch_eles[0].click()
        time.sleep(2)
        self.click_element(ProductManagementLocator.switch_select, doc)
        self.wait_elevisble(ProductManagementLocator.switch_option, doc)
        switch_eles = self.get_elements(ProductManagementLocator.switch_option, doc)
        switch_eles[0].click()
        time.sleep(2)
        self.click_element(ProductManagementLocator.security_group_select, doc)
        self.wait_elevisble(ProductManagementLocator.security_group_option, doc)
        switch_eles = self.get_elements(ProductManagementLocator.security_group_option, doc)
        switch_eles[0].click()
        self.send_keys(ProductManagementLocator.login_password_input, 'asdQWE123~!@', doc)
        self.send_keys(ProductManagementLocator.confirm_password_input, 'asdQWE123~!@', doc)
        self.button_is_enabled(ProductManagementLocator.buy_now_button)
        time.sleep(5)
        self.click_element(ProductManagementLocator.term_of_service, doc)
        time.sleep(2)
        self.wait_elevisble(ProductManagementLocator.pay_button, doc)
        time.sleep(2)
        self.click_element(ProductManagementLocator.pay_button, doc)
        self.wait_elevisble(ProductManagementLocator.pay_successful, doc)
        self.driver.close()
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[0])

    # TL562036 | 购买ECS后，查看Data disks和server guard的日期显示
    def check_ecs_expired_time(self):
        doc = '购买ECS后，查看Data disks和server guard的日期显示'
        product_name = 'Server Guard'
        time.sleep(2)
        self.click_element(ProductManagementLocator.reset_button, doc)
        time.sleep(2)
        expired_time = self.get_element(UserAddExpiredDate.ecs_expired_time_list, doc).text
        time.sleep(2)
        self.wait_elevisble(UserMyResourceLocator.search_select, doc)
        self.click_element(UserMyResourceLocator.search_select, doc)
        time.sleep(2)
        self.wait_elevisble(UserMyResourceLocator.product_name_select, doc)
        self.click_element(UserMyResourceLocator.product_name_select, doc)
        time.sleep(2)
        self.send_keys(UserMyResourceLocator.product_name_input, product_name, doc)
        time.sleep(2)
        self.click_element(UserMyResourceLocator.first_product_name_select, doc)
        time.sleep(2)
        self.click_element(UserMyResourceLocator.orgnazation_search, doc)
        time.sleep(2)
        self.check_attribute(UserAddExpiredDate.ecs_expired_time_list,expired_time)


    def modify_ecs_expired_time(self):
        doc = '更改ECS后，查看Data disks和server guard的日期显示'
        product_name = 'Server Guard'
        time.sleep(2)
        self.click_element(ProductManagementLocator.reset_button, doc)
        time.sleep(2)
        self.wait_elevisble(UserAddExpiredDate.input_new_expired_time, doc)
        self.send_keys(UserAddExpiredDate.input_new_expired_time, '22-10-2030',doc)
        time.sleep(2)
        self.click_element(UserAddExpiredDate.ok_button, doc)
        time.sleep(2)
        new_expired_time = self.get_element(UserAddExpiredDate.ecs_expired_time_list, doc).text
        self.wait_elevisble(UserMyResourceLocator.search_select, doc)
        self.click_element(UserMyResourceLocator.search_select, doc)
        time.sleep(2)
        self.wait_elevisble(UserMyResourceLocator.product_name_select, doc)
        self.click_element(UserMyResourceLocator.product_name_select, doc)
        time.sleep(2)
        self.send_keys(UserMyResourceLocator.product_name_input, product_name, doc)
        time.sleep(2)
        self.click_element(UserMyResourceLocator.first_product_name_select, doc)
        time.sleep(2)
        self.click_element(UserMyResourceLocator.orgnazation_search, doc)
        time.sleep(2)
        self.check_attribute(UserAddExpiredDate.ecs_expired_time_list, new_expired_time)



    # TL562033 | 查看日历插件显示
    def resource_select_expired_time(self):
        self.enter_my_resource_page()
        time.sleep(2)
        doc = '购买时选择PAYG方式，查看页面显示'
        resource_list = ['Elastic IP Addresses', 'Server Load Balancer', 'Elastic Compute Service', 'NAT Gateways',
                         'Block Storage', 'VPN Gateways', 'RDS for MySQL', 'RDS for SQL Server']
        now = datetime.datetime.now()
        year = str(now.year+3)
        for resource in resource_list:
            self.open_pay_pages(resource)
            self.wait_elevisble(UserAddExpiredDate.date_normal, doc)
            text = self.get_element(UserAddExpiredDate.date_normal, doc).text
            self.check_contain_str(text,year)
            time.sleep(2)
            self.keyboard_event(UserAddExpiredDate.expired_time_input, doc)
            self.send_keys(UserAddExpiredDate.expired_time_input,'31-12-2038',doc)
            time.sleep(2)
            self.driver.close()
            all_handles = self.driver.window_handles
            self.driver.switch_to.window(all_handles[0])










